var arr = new Array();
var isall=false;
var role;
$(function(){
	
	/**
	 * 获取风琴菜单
	 */
	$.ajax({
         type: 'GET',
         dataType: "json",
         url: '/roleuser/getRole',
         success: function (data) {
//        	 $('#roleuser').accordion('add', {
//        	        title: '角色管理',
//        	        id:'role',
//        	        selected: true
//        	   });
             $.each(data, function (i, m) {
            	var ahtml = '<ul class="ulcls" id="'+m.roleid+'">';
    			 ahtml+='<li  class="selected"><a href="javascript:void(0);" class="roleusera" onclick="selectRole(\''+m.roleid+'\')">'+m.rolename+'</a></li>';
    			 ahtml+='</ul>';
				 $("#roleuser").append(ahtml);
				 if(i==0){
					 role=m.roleid;
					 $("#"+m.roleid).addClass('select');
					 getRoleOperator(m.roleid);
				 }
             });
         }
     });
	var rightdiv=$(".oper_div").width()-20;
	var grid=$("#operator").datagrid({
		url:'/operator/getUseOperator',
		title:'用户列表',
		method:'get',
		//pagination:true,
		//pageList: [1,20,50,200],
		loadMsg:'加载中......',
		height:450,
		width:rightdiv,
		idField:'operatorid',
		rownumbers:true,
		columns:[[
		         {field:'ck',checkbox:true},
				 {field:'operatorid',title:'操作员编号', align:'left',width:100,sortable:true},
		         {field:'operatorname',title:'操作员名称', align:'left',width:100,sortable:true},
		         {field:'operatoraddress',title:'操作员地址',align:'left',width:250},
		         {field:'operatormobile',title:'联系电话',align:'left',width:80,},
		         {field:'isflag',title:'状态',align:'left',width:80,formatter:function(value,row,index){
		        	if(value){
		        		return '已停用';
		        	}
		        	else{
		        		return '已启用';
		        	}
		         }}
		     ]],
	});
	$("#operator").datagrid({
		toolbar: '#tb'
	});
	$("#serbutton").click(function(){
		var vo={};
		vo.isflag=$("#isflag").val();
		vo.operatorname=$("#operatorname").val();
		vo.operatorid=$("#operatorid").val();
		$("#operator").datagrid('load',vo);
	});
})
/**
 * 选择角色
 * @param roleid
 */
function selectRole(roleid){
	$("ul").removeClass("select");
	$("#"+roleid).addClass('select');
	role=roleid;
	var vo={};
	vo.isflag=$("#isflag").val();
	vo.operatorname=$("#operatorname").val();
	vo.operatorid=$("#operatorid").val();
	$("#operator").datagrid('load',vo);
	getRoleOperator(roleid);
}
function getRoleOperator(roleid){
	if(roleid==null||roleid==''){
		$.messager.alert("失败",'角色id为空','error');
	}
	$.ajax({
		type:'get',
		url: '/roleuser/getRoleOperator',
		data:{roleid:roleid},
		success:function(data){
			if(data.code==200){
				$("#operator").datagrid('unselectAll');
				arr= data.results;
				for(var i=0;i<arr.length;i++){
					$("#operator").datagrid('selectRecord',arr[i]);
				}
			}
			else{
				operator=null;
				$.messager.alert("失败",data.msg,'error');
			}
		},
		error:function(data){
			$.messager.alert("失败","查询失败：",'error');
		}
	});
}
/**
 * 筛选选中角色的用户
 */
function searchOperator(isrole){
	if(role==null||role==''){
		$.messager.alert("失败","未选中角色：",'error');
	}
	var vo={};
	vo.isflag=$("#isflag").val();
	vo.operatorname=$("#operatorname").val();
	vo.operatorid=$("#operatorid").val();
	if(isrole){
		vo.roleid=role;
	}
	$("#operator").datagrid('load',vo);
}
/**
 * 保存
 */
function save(){
	var rows=$("#operator").datagrid('getSelections');
	var ids='';
	for(var i=0;i<rows.length;i++){
		ids+=rows[i].operatorid+',';
	}
	var vo={};
	vo.roleid=role;
	vo.ids=ids;
	$.ajax({
		type:'post',
		url: '/roleuser/saveRoleUser',
		data:vo,
		success:function(data){
			if(data.code==200){
				$.messager.alert("成功","保存成功",'info',function(){
					$("#listOper").datagrid('load',null);
					getRoleOperator(role);
				});
			}
			else{
				$.messager.alert("失败","保存失败："+data.msg,'error');
			}
		},
		error:function(data){
			$.messager.alert("失败","保存失败：",'error');
		}
	});
}


